package com.yyk.yykywd.ui.mine;

import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.donkingliang.labels.LabelsView;
import com.orhanobut.hawk.Hawk;
import com.orhanobut.logger.Logger;
import com.yyk.yykywd.BusinessPeopleInfoKt;
import com.yyk.yykywd.R;
import com.yyk.yykywd.base.BaseActivity;
import com.yyk.yykywd.http.ApiService;
import com.yyk.yykywd.bean.CenterFeedInfo;
import com.yyk.yykywd.util.Md5Util2;
import com.yyk.yykywd.util.OnClickFlagUtil;
import com.yyk.yykywd.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 需求反馈
 */
public class DemandFeedbackActivity extends BaseActivity {


    @BindView(R.id.labels)
    LabelsView labels;
    @BindView(R.id.bt_commit)
    Button btCommit;
    @BindView(R.id.et_content)
    EditText etContent;
    private int type = 1; //默认系统错误


    @Override
    public int getLayoutId() {
        return R.layout.activity_demand_feedback;
    }

    @Override
    public void initView() {
        initToolbar();

    }

    private void initToolbar() {
        setBackArrow();
        setTitle("需求反馈");
    }

    @Override
    public void initData() {
        List<String> list = new ArrayList<>();
        list.add("系统错误");
        list.add("优化建议");
        list.add("新需求");
        //标签
        labels.setLabels(list, new LabelsView.LabelTextProvider<String>() {
            @Override
            public CharSequence getLabelText(TextView label, int position, String data) {
                //根据data和position返回label需要显示的数据。
                return data;
            }
        });

        //标签的选中监听
        labels.setOnLabelSelectChangeListener(new LabelsView.OnLabelSelectChangeListener() {
            @Override
            public void onLabelSelectChange(TextView label, Object data, boolean isSelect, int position) {
                //label是被选中的标签，data是标签所对应的数据，isSelect是是否选中，position是标签的位置。
                type = position + 1;
//                ToastUtil.showShort("type=" + type);

            }
        });


    }


    @OnClick(R.id.bt_commit)
    public void onViewClicked() {
        if (OnClickFlagUtil.isFastClick()) {
            return;
        }

        if (TextUtils.isEmpty(etContent.getText())) {
            ToastUtil.showShort("请输入反馈信息");
            return;
        }
        Map<String, String> map = new TreeMap<>();
        map.put("uid", BusinessPeopleInfoKt.getUid());
        map.put("type", type + "");
        map.put("content", etContent.getText().toString().trim() + "");
        map.put("sign", Md5Util2.createSign(map));
        ApiService.getInstance().api.postCenterFeed(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Observer<CenterFeedInfo>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(CenterFeedInfo info) {
                        Logger.e("需求反馈" + info.toString());
                        ToastUtil.showShort(info.getMsg());
                        if (info.getCode() == 1) {
                            finish();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


}
